package com.meta.nd.util;

import com.meta.common.ConstDef;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NdDbMapping {
	private String dbMapCode;
	private String name;
	private String description;
	private String selectSql;
	private String updateSql;
	private String insertSql;
	private String nvTableName;
	private String fvTableName;
	private List<String> nvFieldNames = new ArrayList<String>();
	private List<String> fvFieldNames = new ArrayList<String>();
	private boolean persistRemainFields = false;
	private Date updateTime;
	

	public NdDbMapping(){

	}
	public NdDbMapping(String dbMapCode, String nvTableName, String fvTableName, String strNvFieldNames, String strFvFieldNames, boolean persistRemainFields) {
		this.dbMapCode = dbMapCode;
		this.nvTableName = nvTableName;
		this.fvTableName = fvTableName;
		this.persistRemainFields = persistRemainFields;
		if ( strNvFieldNames != null && !strNvFieldNames.trim().equals("")) {
			String[] strs = strNvFieldNames.split(",");
			for ( String str : strs ) this.nvFieldNames.add(str);
		}
		if ( strFvFieldNames != null && !strFvFieldNames.trim().equals("")) {
			String[] strs = strFvFieldNames.split(",");
			for ( String str : strs )
				this.fvFieldNames.add(str);
		}
		this.selectSql = "select %s,%s,%s";
		this.selectSql  = String.format(this.selectSql, ConstDef.FD_NAME_ID, ConstDef.FD_NAME_META_ND_CODE, ConstDef.FD_NAME_PARENT_ID);
		
		for (String nvFieldName : this.nvFieldNames  ) {
			this.selectSql += "," + nvFieldName;
		}
		this.selectSql += " from "+ this.nvTableName;
		this.updateSql = "update " + this.nvTableName + " set %s=:%s,%s= :%s ";
		this.updateSql = String.format(this.updateSql, ConstDef.FD_NAME_META_ND_CODE, ConstDef.FD_NAME_META_ND_CODE, ConstDef.FD_NAME_PARENT_ID, ConstDef.FD_NAME_PARENT_ID);
		for (String nvFieldName : this.nvFieldNames ) {
			this.updateSql += ","+ nvFieldName + "= :" + nvFieldName;
		}
		this.updateSql += String.format(" where %s = :%s ", ConstDef.FD_NAME_ID, ConstDef.FD_NAME_ID);
		StringBuffer insertBuffer = new StringBuffer("insert into " + this.nvTableName + String.format("(%s,%s,%s", ConstDef.FD_NAME_ID, ConstDef.FD_NAME_META_ND_CODE, ConstDef.FD_NAME_PARENT_ID));
		for (String nvFieldName : this.nvFieldNames ) {
			insertBuffer.append( "," + nvFieldName + " ");
		}
		insertBuffer.append(String.format(") values(:%s,:%s,:%s", ConstDef.FD_NAME_ID, ConstDef.FD_NAME_META_ND_CODE, ConstDef.FD_NAME_PARENT_ID) );
		for (String nvFieldName : this.nvFieldNames ) {
			insertBuffer.append( ",:" + nvFieldName + " ");
		}
		insertBuffer.append(")");
		this.insertSql = insertBuffer.toString();
	}
	
	public String getDbMapCode() {
		return dbMapCode;
	}
	public void setDbMapCode(String dbMapCode) {
		this.dbMapCode = dbMapCode;
	}
	public String getSelectSql() {
		return selectSql;
	}
	public void setSelectSql(String selectSql) {
		this.selectSql = selectSql;
	}
	public String getUpdateSql() {
		return updateSql;
	}
	public void setUpdateSql(String updateSql) {
		this.updateSql = updateSql;
	}
	
	public String getInsertSql() {
		return insertSql;
	}
	public void setInsertSql(String insertSql) {
		this.insertSql = insertSql;
	}
	public String getNvTableName() {
		if (nvTableName == null)
			return null;
		else
			return nvTableName.toLowerCase();
	}
	public void setNvTableName(String nvTableName) {
		if (nvTableName == null)
			this.nvTableName = null;
		else
			this.nvTableName = nvTableName.toLowerCase();
	}
	public String getFvTableName() {
		if (fvTableName == null)
			return null;
		else
			return fvTableName.toLowerCase();
	}
	public void setFvTableName(String fvTableName) {
		if  (fvTableName == null )
			this.fvTableName = null;
		else
			this.fvTableName = fvTableName.toLowerCase();
	}
	public List<String> getNvFieldNames() {
		return nvFieldNames;
	}
	public void setNvFieldNames(List<String> nvFieldNames) {
		this.nvFieldNames = nvFieldNames;
	}
	public List<String> getFvFieldNames() {
		return fvFieldNames;
	}
	public boolean isFvField( String fdName ){
		if (fdName.equals(ConstDef.FD_NAME_ID) ||
			fdName.equals(ConstDef.FD_NAME_PARENT_ID)
			|| fdName.equals(ConstDef.FD_NAME_META_ND_CODE))
			return false;
		if ( nvFieldNames.contains(fdName))
			return false;
		if (fvFieldNames.contains(fdName))
			return true;
		else if ( this.persistRemainFields)
			return true;
		else
			return false;
	}
	public void setFvFieldNames(List<String> fvFieldNames) {
		this.fvFieldNames = fvFieldNames;
	}
	public boolean isPersistRemainFields() {
		return persistRemainFields;
	}
	public void setPersistRemainFields(boolean persistRemainFields) {
		this.persistRemainFields = persistRemainFields;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	public static String getFvDbNameByDataType(String dataType){
		if ( dataType.equals(ConstDef.DATA_TYPE_LONG)) {
			return "intValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_INT )) {
			return "intValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_FLOAT)){
			return "floatValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_STRING)){
			return "stringValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_BOOLEAN)){
			return "boolValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_DATE)){
			return "dateValue";
		} else if ( dataType.equals(ConstDef.DATA_TYPE_DATETIME)){
			return "dateValue";
		} else if (dataType.equals(ConstDef.DATA_TYPE_STRING_LIST)){
			return "stringValue";
		} else {
			throw new RuntimeException("不存在的数据类型：" + dataType);
		}
	}

}
